﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace UdpSocketClientAsync {

    internal class SocketStats {

        #region Properties 

        private int _totalSendTo;
        internal int TotalSendTo {
            get {
                return this._totalSendTo;
            }
        }

        private int _totalReceiveFrom;
        internal int TotalReceiveFrom {
            get {
                return this._totalReceiveFrom;
            }
        }

        private int _totalReceiveTimeout;
        internal int TotalReceiveTimeout {
            get {
                return this._totalReceiveTimeout;
            }
        }

        private int _responseSuccess;
        internal int ResponseSuccess {
            get {
                return this._responseSuccess;
            }
        }

        private int _adressAlreadyInUse;
        internal int AdressAlreadyInUse {
            get {
                return this._adressAlreadyInUse;
            }
        }

        private int _portCollision;
        internal int PortCollision {
            get {
                return this._portCollision;
            }
        }

        private long _startedAt;
        internal DateTime StartedAt {
            get {
                return DateTime.FromBinary(this._startedAt);
            }
        }

        #endregion

        #region Ctor

        public SocketStats() {
            this.ResetStats();
        }

        #endregion

        internal void ResetStats() {
            lock (this) {
                Interlocked.Exchange(ref this._totalSendTo, 0);
                Interlocked.Exchange(ref this._totalReceiveFrom, 0);
                Interlocked.Exchange(ref this._totalReceiveTimeout, 0);
                Interlocked.Exchange(ref this._responseSuccess, 0);
                Interlocked.Exchange(ref this._adressAlreadyInUse, 0);
                Interlocked.Exchange(ref this._portCollision, 0);
                Interlocked.Exchange(ref this._startedAt, DateTime.Now.ToBinary());
            }
        }

        internal void IncrementTotalSendTo() {
            Interlocked.Increment(ref this._totalSendTo);
        }
        internal void IncrementTotalReceiveFrom() {
            Interlocked.Increment(ref this._totalReceiveFrom);
        }
        internal void IncrementTotalReceiveTimeout() {
            Interlocked.Increment(ref this._totalReceiveTimeout);
        }
        internal void IncrementResponseSuccess() {
            Interlocked.Increment(ref this._responseSuccess);
        }
        internal void IncrementAdressAlreadyInUse() {
            Interlocked.Increment(ref this._adressAlreadyInUse);
        }
        internal void IncrementPortCollision() {
            Interlocked.Increment(ref this._portCollision);
        }

        
    }
}
